// pages/user/address/addressList/addressList.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        addressListType: null,
        addressList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            addressListType: options.type
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getAddressList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getAddressList: function() {
        const that = this
        wx.showLoading({
            title: '加载中',
        })
        let data = {
            
        }
        common.post('Address/address_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    addressList: []
                })
            } else {
                that.setData({
                    addressList: res
                })
            }
        }).catch(function(e) {})
    },

    goAddAddress: function() {
        wx.navigateTo({
            url: '/pages/user/address/addressAdd/addressAdd'
        })
    },

    goAddressEdit: function(e) {
        wx.setStorageSync('editAddress', e.currentTarget.dataset.address)
        wx.navigateTo({
            url: '/pages/user/address/addressEdit/addressEdit'
        })
    },

    addressSelect: function(e) {
        if (this.data.addressListType == 'readOnly') {
            return
        } else {
            wx.setStorageSync('selectAddress', e.currentTarget.dataset.address)
            wx.navigateBack({
                delta: 1
            })
        }
    }
})